#! /usr/bin/python
import sys
from TOSSIM import *
#from RadioCountMsg import *
from tinyos.tossim.TossimApp import *
import random

n = NescApp()
t = Tossim(n.variables.variables())
#t = Tossim([])
m = t.mac();
r = t.radio();

t.addChannel("RadioSenseToLeds", sys.stdout);
t.addChannel("LedsC", sys.stdout);
t.addChannel("DosmfSenderP", sys.stdout);
t.addChannel("DosmfReceiverP", sys.stdout);
#t.addChannel("AMQueue", sys.stdout);

for i in range(0, 4):
  m = t.getNode(i);
  m.bootAtTime((31 + t.ticksPerSecond() / 10) * i + 1);

f = open("topo.txt", "r")
lines = f.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
    if (s[0] == "gain"):
      r.add(int(s[1]), int(s[2]), float(s[3]))

noise = open("meyer-heavy.txt", "r")
lines = noise.readlines()
for line in lines:
  str = line.strip()
  if (str != ""):
    val = int(str)
    for i in range(0, 4):
      t.getNode(i).addNoiseTraceReading(val)

for i in range (0, 4):
  t.getNode(i).createNoiseModel()

for i in range(0, 60):
  t.runNextEvent();


"""
msg = RadioCountMsg()
pkt = t.newPacket()	
pkt.setType(msg.get_amType())

#print "Delivering " + str(msg) + " to 0 at " + str(t.time() + 3);
#for i in range(1, 4):
m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(v.getData())
pkt.setDestination(1)
pkt.setData(msg.data)
pkt.deliver(1, t.time())


for i in range(0, 30):
  t.runNextEvent();

m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(v.getData())
pkt.setDestination(2)
pkt.setData(msg.data)
pkt.deliver(2, t.time())


for i in range(0, 30):
  t.runNextEvent();

m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(v.getData())
pkt.setDestination(3)
pkt.setData(msg.data)
pkt.deliver(3, t.time())
"""

for i in range(0, 100):
  t.runNextEvent();


